package cn.tiga.pet.controller;

import cn.tiga.basic.annotation.PreAuthorize;
import cn.tiga.basic.util.LoginContext;
import cn.tiga.pet.service.IPetService;
import cn.tiga.pet.domain.Pet;
import cn.tiga.pet.query.PetQuery;
import cn.tiga.basic.util.JsonResult;
import cn.tiga.basic.util.PageList;
import cn.tiga.user.domain.Logininfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;


    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @PreAuthorize(name = "宠物新增修改",sn = "pet:put")
    public JsonResult addOrUpdate(@RequestBody Pet pet){
        try {
            if( pet.getId()!=null)
                petService.update(pet);
            else
                petService.add(pet);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @PreAuthorize(name = "宠物根据id删除",sn = "pet:delete")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            petService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return JsonResult.me().setMsg("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "宠物根据id查询",sn = "pet:get")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @PreAuthorize(name = "宠物查询全部",sn = "pet:get")
    public List<Pet> list(HttpServletRequest request){
        Logininfo loginInfo = LoginContext.getLoginInfo(request);
        return petService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @PreAuthorize(name = "宠物分页查询",sn = "pet:post")
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        return petService.pageQuery(query);
    }

    @PostMapping("/onsale")
    public JsonResult onSale(@RequestBody List<Long> ids ){
        try {
            petService.onSale(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
        return new JsonResult().setMsg("上架失败请稍后重试");
        }
    }

    @PostMapping("/offsale")
    @ApiOperation(value = "下架")
    public JsonResult offSale(@RequestBody List<Long> ids){
        try {
            petService.offSale(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return new JsonResult().setMsg("下架失败请稍后重试");
        }
    }

}
